package com.ugoodtech.mdcc.core.helper;

import com.ugoodtech.mdcc.core.domain.Brand;
import com.ugoodtech.mdcc.core.domain.Nation;
import com.ugoodtech.mdcc.core.domain.QuestionAnswerOption;
import com.ugoodtech.mdcc.core.domain.UserProfile;
import com.ugoodtech.mdcc.core.model.entity.QuestionAnswerOptions;
import com.ugoodtech.mdcc.core.model.entity.QuestionAnswerOptionsExample;
import com.ugoodtech.mdcc.core.model.mapper.QuestionAnswerOptionsMapper;
import com.ugoodtech.mdcc.core.repository.BrandRepository;
import com.ugoodtech.mdcc.core.repository.QuestionAnswerOptionRepository;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import org.springframework.util.CollectionUtils;

/**
 * 封装用户的省份+品牌的查询信息
 */
@Component
public class ProvinceBrandSearchHelper {

    @Autowired
    private BrandRepository brandRepository;

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;

    @Autowired
    private QuestionAnswerOptionsMapper questionAnswerOptionsMapper;

    /**
     * 通过筛选的省份和品牌查询出该账号对应的常规报告ID
     *
     * @param userProfile 用户详情POJO
     * @param province    筛选的省份
     * @param brand       筛选的品牌
     * @return 用户筛选用的省份和品牌相关参数
     */
    public ProvinceBrandPoJo getUserSearchProvinceBrand(UserProfile userProfile, String province, String brand, boolean isColorReport) {
        ProvinceBrandPoJo provinceBrandPoJo = new ProvinceBrandPoJo();
        //获取负责的省份
        List<Nation> nationList = userProfile.getNationList();
        List<String> ownNationStr = new ArrayList<>();
        for (Nation nation : nationList) {
            ownNationStr.add(nation.getProvince());
        }
        provinceBrandPoJo.setOwnProvinceList(ownNationStr);

        //获取有权限筛的选省份
        List<String> canProvinceList = new ArrayList<>();
        if (StringUtils.isNotEmpty(province)) {
            String[] provinceArray = province.split(",");
            //筛选的省份
            provinceBrandPoJo.setSearchProvinceList(provinceArray);
            for (String s : provinceArray) {
                if (ownNationStr.contains(s)) {
                    canProvinceList.add(s);
                }
            }
        }
        provinceBrandPoJo.setCanProvinceList(canProvinceList);

        //获取负责的品牌
        List<Brand> ownBrandList = userProfile.getBrandList();
        //负责的品牌转为String集合
        List<String> ownBrandStr = new ArrayList<>();
        for (Brand b : ownBrandList) {
            ownBrandStr.add(b.getBrandName());
        }
        //Brand以基础报告来查询
        if (isColorReport) {
            //如果权限有筛选品牌
            List<Brand> canBrandList = new ArrayList<>();
            List<Brand> searchBrandList = new ArrayList<>();
            if (StringUtils.isNotEmpty(brand)) {
                String[] brandArray = brand.split(",");
                List<Brand> brands = brandRepository.selectByNameList(Arrays.asList(brandArray));
                Map<String, Brand> nameToBrandMap = brands.stream()
                    .collect(Collectors.toMap(Brand::getBrandName, Function.identity()
                        , (s1, s2) -> s1));
                for (String s : brandArray) {
                    Brand brandPojo = nameToBrandMap.get(s);
                    if (null != brandPojo) {
                        searchBrandList.add(brandPojo);
                        if (ownBrandStr.contains(s)) {
                            canBrandList.add(brandPojo);
                        }
                    }
                }
            }
            List<Long> ownBrandId = getQuestionAnswer(ownBrandList);
            List<Long> canBrandLId = getQuestionAnswer(canBrandList);
            List<Long> searchBrandId = getQuestionAnswer(searchBrandList);
            provinceBrandPoJo.setOwnBrandList(ownBrandId);
            provinceBrandPoJo.setCanBrandList(canBrandLId);
            provinceBrandPoJo.setSearchBrandList(searchBrandId);
        } else {
            //Brand以投诉报告查询
            if (StringUtils.isNotEmpty(brand)) {
                List<String> canBrandList = new ArrayList<>();
                List<String> searchBrandList = getQuestionAnswer(brand);
                for (String brandStr : searchBrandList) {
                    if (ownBrandStr.contains(brandStr)) {
                        canBrandList.add(brandStr);
                    }
                }
                List<Long> canBrandLId = getComplaintQuestionAnswer(canBrandList);
                provinceBrandPoJo.setCanBrandList(canBrandLId);
                List<Long> searchBrandId = getComplaintQuestionAnswer(searchBrandList);
                provinceBrandPoJo.setSearchBrandList(searchBrandId);
            }
            List<Long> ownBrandId = getComplaintQuestionAnswer(ownBrandStr);
            provinceBrandPoJo.setOwnBrandList(ownBrandId);

        }
        return provinceBrandPoJo;
    }

    private List<Long> getQuestionAnswer(List<Brand> brandList) {
        // List<Long> answerId = new ArrayList<>();
        // for (Brand brand : brandList) {
        //     QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findByName(brand.getBrandName());
        //     if (null != questionAnswerOption) {
        //         answerId.add(questionAnswerOption.getId());
        //     }
        // }
        if (CollectionUtils.isEmpty(brandList)) {
            return new ArrayList<>(0);
        }
        List<String> nameList = brandList.stream().map(Brand::getBrandName)
            .collect(Collectors.toList());
        QuestionAnswerOptionsExample example = new QuestionAnswerOptionsExample();
        example.createCriteria().andDeletedEqualTo(false).andEnabledEqualTo(true)
            .andQuestionIdEqualTo(8L).andNameIn(nameList);
        List<QuestionAnswerOptions> questionAnswerOptions = questionAnswerOptionsMapper.selectByExample(
            example);
        return questionAnswerOptions.stream().map(QuestionAnswerOptions::getId).collect(Collectors.toList());
    }

    private List<Long> getComplaintQuestionAnswer(List<String> brandStrList) {
        // List<Long> answerId = new ArrayList<>();
        // for (String brand : brandStrList) {
        //     QuestionAnswerOption questionAnswerOption =
        //             questionAnswerOptionRepository.findComplaintBrandByName(brand);
        //     if (null != questionAnswerOption) {
        //         answerId.add(questionAnswerOption.getId());
        //     }
        // }
        if (CollectionUtils.isEmpty(brandStrList)) {
            return new ArrayList<>(0);
        }
        QuestionAnswerOptionsExample example = new QuestionAnswerOptionsExample();
        example.createCriteria().andDeletedEqualTo(false).andEnabledEqualTo(true)
            .andQuestionIdEqualTo(194L).andNameIn(brandStrList);
        List<QuestionAnswerOptions> questionAnswerOptions = questionAnswerOptionsMapper.selectByExample(
            example);
        return questionAnswerOptions.stream().map(QuestionAnswerOptions::getId).collect(Collectors.toList());
    }

    //获取投诉报告查询品牌时，再封装一次拿到名称
    private List<String> getQuestionAnswer(String brandIds) {
        List<String> answer = new ArrayList<>();
        String[] brandId = brandIds.split(",");
        for (String id : brandId) {
            QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
            if (null != questionAnswerOption&&questionAnswerOption.getName()!=null) {
                answer.add(questionAnswerOption.getName());
            }
        }
        return answer;
    }


}
